package com.hake.example.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;
import com.hake.core.exception.AppException;
import com.hake.core.exception.BusinessException;
import com.hake.core.exception.DataBaseException;
import com.hake.example.entity.User;
import com.hake.example.entity.UserContact;
import com.hake.example.service.UserContactService;
import com.hake.example.service.UserInfoService;
import com.hake.example.service.UserService;
import com.hake.example.web.dto.UserInfo;

@Service
public class UserInfoServiceImpl implements UserInfoService {

	private final Logger logger = LoggerFactory.getLogger(this.getClass()) ;
	
	@Autowired
	private UserService userService;

	@Autowired
	private UserContactService contactService;

	@Transactional
	@Override
	public void addUserInfo(User user, UserContact contact) throws AppException, BusinessException {
		try {
			this.userService.insertSelective(user);

			int i = 0;
			if (i == 0) {
				throw new BusinessException("发生业务异常");
			}

			contact.setUserId(user.getId());
			this.contactService.insertSelective(contact);

		} catch (BusinessException e) {
			//必须每个异常都进行捕获再抛出，不然的话将统一抛出AppException异常
			throw e;
		} catch (Exception e) {
			throw new AppException("程序内部异常：" + e.getMessage());
		}
	}
	

	@Override
	public boolean insert(UserInfo dto) throws DataBaseException, BusinessException, AppException {
		try {
			User user = new User() ;
			UserContact contact = new UserContact() ;
			
			BeanUtils.copyProperties(dto.getUser(), user);
			BeanUtils.copyProperties(dto.getContact(), contact);
			
			this.userService.insert(user) ;
			
			contact.setUserId(user.getId());
			this.contactService.insert(contact) ;
			
			return true;
		} catch (BusinessException e) {
			throw new BusinessException(e);
		} catch (DataBaseException e) {
			throw new DataBaseException(e);
		} catch (Exception e) {
			throw new AppException(e);
		}
	}

	@Override
	public boolean delete(UserInfo dto) throws DataBaseException, BusinessException, AppException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(UserInfo dto) throws DataBaseException, BusinessException, AppException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public UserInfo get(Integer id) throws DataBaseException, BusinessException, AppException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> selectList(UserInfo dto) throws DataBaseException, BusinessException, AppException {
		return this.userService.selectList(dto) ;
	}
	
	@Override
	public PageInfo<User> selectPage(UserInfo dto) throws DataBaseException, BusinessException, AppException {
		try {
			PageInfo<User> selectPage = this.userService.selectPage(dto) ;
			
			return selectPage ;
		} catch (DataBaseException e) {
			throw new DataBaseException(e) ; 
		} catch (BusinessException e) {
			throw new BusinessException(e) ; 
		} catch (Exception e) {
			logger.error("内部异常：{}", e);
			throw new AppException(e) ; 
		}
		
	}

	@Override
	public PageInfo<User> selectPage(Map<String, Object> params) throws DataBaseException, BusinessException, AppException {
		try {
			PageInfo<User> selectPage = this.userService.selectPage(params) ;
			
			return selectPage ;
		} catch (DataBaseException e) {
			throw new DataBaseException(e) ; 
		} catch (BusinessException e) {
			throw new BusinessException(e) ; 
		} catch (Exception e) {
			logger.error("内部异常：{}", e);
			throw new AppException(e) ; 
		}
		
	}


}
